package cn.edu.lingnan.fruitshop.controller;


import cn.edu.lingnan.fruitshop.common.CodeUtils;
import cn.edu.lingnan.fruitshop.common.JSONResult;
import cn.edu.lingnan.fruitshop.service.GoodstableService;
import cn.edu.lingnan.fruitshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.edu.lingnan.fruitshop.service.GoodstableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张小凡
 * @since 2022-01-04
 */
@Controller
@RequestMapping("/user")
public class UserController {

    // 注入Service对象
    @Autowired
    private UserService userService;

    @Autowired
    private GoodstableService goodstableService;

    // 检查邮箱是否唯一 以及格式
    @PostMapping("/checkEmail")
    @ResponseBody
    public JSONResult<Void> checkEmail(String email){
        return userService.checkEmail(email);
    }

    // 发送邮箱验证码
    @PostMapping("/sendEmailCode")
    @ResponseBody
    public JSONResult<Void> sendEmailCode(String email, HttpSession session){
        return userService.sendEmailCode(email,session);
    }

    // 进行注册
    @PostMapping("/doReg")
    @ResponseBody
    public JSONResult<Void> doReg(String email,String password,String comfirmPwd,String code,String option,HttpSession session){
        return userService.doReg(email,password,comfirmPwd,code,option,session);
    }

    // 登录时验证邮箱是否存在
    @PostMapping("/checkLoginEmail")
    @ResponseBody
    public JSONResult<Void> checkLoginEmail(String email){
        return userService.checkLoginEmail(email);
    }

    // 页面加载时获取验证码
    @GetMapping("/getLoginCode")
    public void getLoginCode(HttpServletRequest request, HttpServletResponse response){
        CodeUtils.getvalidateCode(request,response);
    }

    // 用户进行登录操作
    @PostMapping("/doLogin")
    @ResponseBody
    public JSONResult<Void> doLogin(String email,String password,String code,HttpSession session){
        return userService.doLogin(email,password,code,session);
    }

    // 用户进行密码重置操作
    @PostMapping("/doResetPwd")
    @ResponseBody
    public JSONResult<Void> doResetPwd(String email,String password,String comfirmPwd,String code,HttpSession session){
        return userService.doResetPwd(email,password,comfirmPwd,code,session);
    }

    // 重置密码时发送邮箱验证码
    @PostMapping("/resetPwdSendEmailCode")
    @ResponseBody
    public JSONResult<Void> resetPwdSendEmailCode(String email,HttpSession session){
        return  userService.resetPwdSendEmailCode(email,session);
    }
    @GetMapping("toList")
    public String toList(Model model, HttpSession session,Integer typeId){
        String view= goodstableService.Seemore(model,session,typeId);
        return view;

    }

}

